package sign_in.loginsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sign_in.loginsystem.model.util.Result;
import sign_in.loginsystem.model.entity.User;
import sign_in.loginsystem.service.port.AccountService;
import sign_in.loginsystem.service.port.AdminService;
import sign_in.loginsystem.tool.Analyze;



@Slf4j
@RestController
public class LoginController {


    @Autowired
    Analyze analyze;


    @Autowired
    AccountService accountService;

    @Autowired
    AdminService adminService;




    @PostMapping("/login")
    public Result login(@RequestBody User user){
        return accountService.login(user);
    }

    @PostMapping("/user")
    public Result userMessage(@RequestHeader("Token") String token){
        User user = analyze.analyzeToken(token);
        if (user==null){
            return Result.error(false);
        }
        return Result.success(user);
    }

    @PostMapping("/del")
    public Result delete(@RequestHeader("Token")String token){
        boolean sure;
        User user = analyze.analyzeToken(token);
        sure = accountService.delete(user);
        return Result.delete(sure);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        return Result.creat(accountService.registerAccount(user));
    }

    @PostMapping("/edit")
    public Result edit(@RequestHeader("Token")String token,@RequestBody User user){
        User analyzed = analyze.analyzeToken(token);
        return accountService.editAccount(analyzed,user) ;
    }



}


